<template>
	<view class="container flex_C">
		<FeNavbar title="" :isBorder="false" />
		<view class="top_wrap flexV_sb">
			<view class="title">{{title}}</view>
			<view class="btn flexHV" @click="onRead">
				<text class="text">全部已读</text>
				<image class="icon" src="/static/common/del_icon.png" mode=""></image>
			</view>
		</view>
		<view class="flex_1">
			<z-paging :fixed="false" ref="paging" v-model="dataList" @query="queryList"
				:auto-show-system-loading="true">
				<view class="item_wrap flex" v-for="(item,index) in dataList" :key="index" @click="toDetails(item)">
					<image class="icon" :src="item.iconUrl" mode=""></image>
					<view class="flex_1">
						<view class="top_box flexV_sb">
							<text class="title">{{item.title}}</text>
							<text class="time">{{timeFormat(item.createTime)}}</text>
						</view>
						<view class="content_wrap flexV">
							<view class="content van_ellipsis">{{item.summary}}</view>
							<view class="read" v-if="!item.isRead"></view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
	</view>
</template>

<script>
	import {
		messageTypeApi,
		readApi
	} from '@/api/message.js'
	export default {
		data() {
			return {
				type: '',
				title: '',
				dataList: []
			}
		},
		onLoad(o) {
			this.type = o.type;
			this.title = o.title;
		},
		methods: {
			queryList(pageNo, pageSize) {
				messageTypeApi(this.type, {
					page: pageNo,
					limit: pageSize
				}).then(res => {
					console.log(res, '1111111')
					// 将请求结果通过complete传给z-paging处理，同时也代表请求结束，这一行必须调用
					this.$refs.paging.complete(res.data.messages);
				}).catch(res => {
					console.log(res)
					// 如果请求失败写this.$refs.paging.complete(false)，会自动展示错误页面
					// 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
					// 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
					this.$refs.paging.complete(false);
				})
			},
			timeFormat(time) {
				const now = Date.parse(new Date())
				const diff = (now / 1000) - time
				if (diff < 60) {
					return `${diff}秒前`
				} else if (diff < (60 * 60)) {
					return `${Math.floor(diff/60)}分钟前`
				} else if (diff < (60 * 60 * 24)) {
					return `${Math.floor(diff/60/60)}小时前`
				}
				return `${Math.floor(diff/60/60/24)}天前`
			},

			async onRead(data = {}, load = true) {
				load && uni.showLoading({
					mask: true
				})
				const res = await readApi({
					categoryType: this.type,
					...data
				})
				load && uni.hideLoading()
				if (res.code == 200) {
					this.$refs.paging.reload()
					load && uni.showToast({
						title: '已读完成',
						icon: 'success'
					})
				}
			},
			toDetails(item) {
				const {
					contentUrl,
					message_id
				} = item;
				this.onRead({
					messageId: message_id
				}, false)

				uni.navigateTo({
					url: contentUrl ?
						'/pages/common/webView?src=' + item.contentUrl :
						`/pages/common/notificationContent?id=${message_id}&type=${this.type}`
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		height: 100vh;

		&>.flex_1 {
			height: 0;
		}

		.top_wrap {
			padding: 32rpx;

			.title {
				font-weight: 600;
				font-size: 50rpx;
				color: #26282B;
			}

			.btn {
				width: 160rpx;
				height: 54rpx;
				background: #EEF1F6;
				border-radius: 200rpx 200rpx 200rpx 200rpx;

				.text {
					font-weight: 400;
					font-size: 24rpx;
					color: #A6ACB4;
				}

				.icon {
					width: 24rpx;
					height: 24rpx;
					margin-left: 8rpx;
				}
			}
		}

		.item_wrap {
			padding: 16rpx 32rpx;
			padding-bottom: 0;

			&>.flex_1 {
				overflow: hidden;
			}

			.icon {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				overflow: hidden;
				background-color: #7B828A;
				margin-right: 24rpx;
			}

			.top_box {
				margin-bottom: 12rpx;

				.title {
					ffont-weight: 600;
					font-size: 30rpx;
					color: #7B828A;
				}

				.time {
					font-weight: 400;
					font-size: 24rpx;
					color: #A6ACB4;
					margin-left: 24rpx;
				}
			}

			.content_wrap {
				margin-top: 12rpx;
				overflow: hidden;
				padding-bottom: 16rpx;
				border-bottom: 1px solid #DBE0E6;
			}

			.content {
				flex: 1;
				font-weight: 400;
				font-size: 26rpx;
				color: #A6ACB4;
			}

			.read {
				width: 18rpx;
				height: 18rpx;
				background: #F04848;
				border-radius: 50%;
				margin-left: 24rpx;
			}
		}
	}
</style>